// EquipFormatAssembler.cpp: implementation of the EquipFormatAssembler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EquipFormatAssembler.h"



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

EquipFormatAssembler::EquipFormatAssembler()
{

}

EquipFormatAssembler::~EquipFormatAssembler()
{

}


EquipFormatAssembler* EquipFormatAssembler::Instance()
{
	static EquipFormatAssembler assembler;
	return &assembler;
}

FormatPropertyContainer& EquipFormatAssembler::GetFormatPropertyContainer(const StdString& strFEquipName,const StdString& strSEquipName,const StdString& strTable,const StdString& strSource)
{	
	ASSERT(!strFEquipName.IsEmpty());
	ASSERT(!strSEquipName.IsEmpty());
	ASSERT(!strTable.IsEmpty());
	ASSERT(!strSource.IsEmpty());

	StdString strKey = strFEquipName + strSEquipName;

	FormatPropertyContainer* pContainer = NULL;
	pContainer = FindFormatPropertyContainer(strKey);
	if(pContainer != NULL)
	{
		return *pContainer;
	}
	pContainer = new FormatPropertyContainer;

	CString strSQL = "select * from "+strTable+" where ID='"+strSEquipName+"'";

	UEConnectionPtr pConnectionPtr = UEConnectionManager::Instance().GetConnection(strSource);
    UERecordsetPtr ptrSet = pConnectionPtr->ExecuteSelectSQL(_T(strSQL));
	ASSERT(!ptrSet->IsEmpty());
	ptrSet->MoveFirst();
	while(!ptrSet->IsEOF())
	{
		FormatProperty* pProperty = NULL;
		pProperty = CreateFormatProperty(ptrSet);
		ASSERT(pProperty != NULL);
		pContainer->AddFormatProperty(pProperty);
		ptrSet->MoveNext();
	}
	Add(strKey,pContainer);

	return *pContainer;

}

FormatPropertyContainer* EquipFormatAssembler::FindFormatPropertyContainer(const StdString& strKey)
{	
	ASSERT(!strKey.IsEmpty());
	FormatPropertyContainer* pFormatContainer = NULL;
	if(strKey.IsEmpty())
		return NULL;

	pFormatContainer = GetManaged(strKey);
	if(pFormatContainer != NULL)
	{
		return pFormatContainer;
	}
	else 
	{
		return pFormatContainer;
	}

	
}

FormatProperty* EquipFormatAssembler::CreateFormatProperty(UERecordsetPtr ptrSet)
{
	FormatProperty* pProperty = new FormatProperty();
	ASSERT(pProperty != NULL);
	pProperty->SetEquipName(ptrSet->GetString(_T("ID")));
	pProperty->SetCondition(ptrSet->GetString(_T("Conditon")));
	pProperty->SetScriptPath(ptrSet->GetString(_T("Path")));
	pProperty->SetSegName(ptrSet->GetString(_T("SegName")));
	pProperty->SetImagePath(ptrSet->GetString(_T("ImagePath")));

	return pProperty;
}